package com.guangzhi.weijianzhi.context;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import cn.jpush.android.api.JPushInterface;

import com.ihgoo.allinone.http.RequestManager;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;

public class WeijianzhiApplication extends Application {
	static Context instance;

	@Override
	public void onCreate() {
		DisplayImageOptions options = new DisplayImageOptions.Builder()
			.displayer(new FadeInBitmapDisplayer(50))
	    	.bitmapConfig(Bitmap.Config.RGB_565)
	    	.imageScaleType(ImageScaleType.EXACTLY) // default
	    	.cacheInMemory(true)
	    	.cacheOnDisk(true)
	    	.build();
		
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
			.memoryCache(new WeakMemoryCache())
			.defaultDisplayImageOptions(options)
			.build();
		
		ImageLoader.getInstance().init(config);
		UmengUpdateAgent.setUpdateCheckConfig(false);//禁用友盟自动检测升级功能
		 JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
         JPushInterface.init(this);     		// 初始化 JPush
         JPushInterface.setAliasAndTags(this,
 				WeiJianZhiEntity.access_token, null);
         MobclickAgent.updateOnlineConfig(this);
		try {
			Class.forName("android.os.AsyncTask");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		super.onCreate();
		instance = getApplicationContext();
		RequestManager.init(this);
//		 CrashHandler crashHandler = CrashHandler.getInstance() ;
//         crashHandler.init(this) ;
	//	HttpRequestUtils.setmHttpUrls(new HttpUrls());
	}


	// 实例对象
	public static WeakReference<Activity> instanceRef;

	public static synchronized Context getInstance() {
		if (instanceRef == null || instanceRef.get() == null) {
			return WeijianzhiApplication.getContext();
		} else {
			return instanceRef.get();
		}
	}

	public static synchronized Activity getActivity() {
		Activity result = null;
		if (instanceRef != null && instanceRef.get() != null) {
			result = instanceRef.get();
		}
		return result;
	}

	public static synchronized Context getContext() {
		return instance;
	}

	public static SparseArray<WeakReference<Activity>> taskStack = new SparseArray<WeakReference<Activity>>();

	public static synchronized SparseArray<WeakReference<Activity>> getTaskStack() {
		return taskStack;
	}
	
	
	
}
